<?php

namespace App\Repository\Repositories;

use App\Repository\Models\ProductSkus;
use App\Repository\Repositories\Interfaces\ProductSkusRepository;

/**
 * Class ProductSkusRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class ProductSkusRepositoryEloquent extends BaseRepository implements ProductSkusRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
        'id' => 'in',
        'product.name'=> 'like',
        'product.brand_id',
        'product.first_cat',
        'product.second_cat',
        'product.third_cat',
        'product.is_for_sale',
        'attributes'=>'like',
        'sale_price' => 'between',
        'product.status',
    ];

    public $fieldSearchReplace = [
        'product_name' => 'product.name',
        'third_cat'=> 'product.third_cat',
        'second_cat'=> 'product.second_cat',
        'first_cat'=> 'product.first_cat',
        'brand_id'=> 'product.brand_id',
        'product_status'=>'product.status',
        'is_for_sale'=>'product.is_for_sale',
    ];
    
    /**
     * 可关联查询的字段
     */
    public $withAllow = [
        'product'
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return ProductSkus::class;
    }

    public function incrementStock($sku_id, $num)
    {
        $ret = $this->query()->where(['id' => $sku_id])->increment('stock', $num);
        return $ret;
    }

    public function decrementStock($sku_id, $num)
    {
        $ret = $this->query()->find($sku_id)->decrement('stock', $num);
        return $ret;
    }

}
